<template>
  <div class="app-container">
    <!-- 上部分表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="60px"
      :label-position="labelPosition"
      class="globalSerchFormStyle"
      style="margin: 10px 0px 15px 0px"
    >
      <el-row>
        <el-col
          :span="6"
          v-if="['', null].includes($store.state.user.userLevel)"
        >
          <el-form-item
            label="油企名称"
            prop="enterpriseId"
            size="small"
            style="padding-top: 4px"
          >
            <el-select
              v-model="queryParams.enterpriseId"
              :clearable="true"
              filterable
              placeholder="请选择油企名称"
              style="margin-left: 10px"
              @change="handleSelectChange"
            >
              <el-option
                v-for="(item, index) in enterpriseList"
                :key="index"
                :value="item.enterpriseId"
                :label="item.enterpriseName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="油站名称"
            prop="stationId"
            clearable
            size="small"
            style="padding-top: 4px"
          >
            <el-select
              v-model="queryParams.stationId"
              :clearable="true"
              filterable
              placeholder="请选择油站名称"
              style="margin-left: 10px"
            >
              <el-option
                v-for="item in stationList"
                :key="item.stationId"
                :label="item.stationName"
                :value="item.stationId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="交易类型"
            prop="cardTypeList"
            clearable
            size="small"
            style="padding-top: 4px"
          >
            <el-select
              v-model="queryParams.cardTypeList"
              placeholder="请选择交易类型"
              style="margin-left: 10px"
              multiple
            >
              <el-option label="后台充值" value="cz"></el-option>
              <el-option label="后台增加" value="imp"></el-option>
              <el-option label="后台扣减" value="msub"></el-option>
              <el-option label="消费核销" value="cdk"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="车队名称"
            prop="fleetCardId"
            clearable
            size="small"
            style="padding-top: 4px"
          >
            <el-select
              v-model="queryParams.fleetCardId"
              :clearable="true"
              filterable
              placeholder="请选择车队名称"
              style="margin-left: 10px"
            >
              <el-option
                v-for="item in rechargeFleetNameList"
                :key="item.id"
                :label="item.fleetName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-show="isHide">
        <el-col :span="6">
          <el-form-item
            label="主卡号"
            prop="parentCardNumber"
            size="small"
            style="padding-top: 4px"
          >
            <el-input
              v-model="queryParams.parentCardNumber"
              placeholder="请输入主卡号"
              style="margin-left: 10px"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item
            label="子卡号"
            prop="sonCardNumber"
            size="small"
            style="padding-top: 4px"
          >
            <el-input
              v-model="queryParams.sonCardNumber"
              placeholder="请输入子卡号"
              style="margin-left: 10px"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="用户姓名"
            prop="userName"
            size="small"
            style="padding-top: 4px"
          >
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入用户姓名"
              style="margin-left: 10px"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="交易单号"
            prop="fleetOrderNo"
            size="small"
            style="padding-top: 4px"
          >
            <el-input
              v-model="queryParams.fleetOrderNo"
              placeholder="请输入交易单号"
              style="margin-left: 10px"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row v-show="isHide">
        <el-col :span="6">
          <el-form-item
            label="交易时间"
            prop="dateRangeCreatedDate"
            size="small"
            style="padding-top: 4px"
          >
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dateRangeCreatedDate"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeDateRangeDate"
              style="width: 100%; margin-left: 10px"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item
            label="手机号"
            prop="userPhone"
            size="small"
            style="padding-top: 4px"
          >
            <el-input
              v-model="queryParams.userPhone"
              placeholder="请输入手机号"
              style="margin-left: 10px"
              clearable
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <div class="button-from"></div>
    </el-form>

    <el-row class="global_el_rowStyle" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-arrow-up"
          size="mini"
          plain
          v-show="isHide"
          @click="isHide = false"
        >
          收起
        </el-button>
        <el-button
          icon="el-icon-arrow-down"
          size="mini"
          plain
          v-show="!isHide"
          @click="isHide = true"
        >
          展开更多
        </el-button>
      </el-col>
    </el-row>

    <div class="summary-container">
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">车队主卡余额总计</span>
          <span class="summary-sum"
            >{{ workSum.parentCardNumberBalance || 0 }}元</span
          >
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">车队子卡分配余额总计</span>
          <span class="summary-sum"
            >{{ workSum.sonCardNumberBalance || 0 }}元</span
          >
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">车队卡消费总计</span>
          <span class="summary-sum"
            >{{ workSum.consumptionBalance || 0 }}元</span
          >
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">车队卡充值总计</span>
          <span class="summary-sum">{{ workSum.rechargeBalance || 0 }}元</span>
        </div>
      </div>
      <div class="summary-item f1">
        <div class="summary-item-box f1">
          <span class="summary-title">车队卡赠送总计</span>
          <span class="summary-sum">{{ workSum.giftBalance || 0 }}元</span>
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <el-table
      border
      style="width: 100%"
      :data="orderList"
      v-loading="loading"
      header-align="left"
    >
      <el-table-column
        label="油企名称"
        prop="enterpriseName"
        align="left"
        width="150"
        v-if="['', null].includes($store.state.user.userLevel)"
      ></el-table-column>
      <el-table-column
        label="油站名称"
        prop="stationName"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column
        label="车队名称"
        prop="fleetName"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column
        label="交易时间"
        prop="createdTime"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column
        label="交易类型"
        prop="cardType"
        align="left"
        width="150"
        :formatter="formatterRefundPay"
      ></el-table-column>
      <el-table-column
        label="交易金额"
        prop="rechargeAmount"
        align="left"
        width="150"
        :formatter="formatRechargeAmount"
      >
      </el-table-column>
      <el-table-column
        label="赠送金额"
        prop="giftAmount"
        align="left"
        width="150"
      >
        <template slot-scope="scope">
          {{ "+" + scope.row.giftAmount }}</template
        ></el-table-column
      >
      <el-table-column
        label="车队卡余额"
        prop="afterBalance"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column
        label="主卡号"
        prop="parentCardNumber"
        align="left"
        width="180"
      ></el-table-column>
      <el-table-column
        label="子卡号"
        prop="sonCardNumber"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column
        label="车队卡用户姓名"
        prop="userName"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column
        label="手机号"
        prop="userPhone"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column
        label="交易单号"
        prop="fleetOrderNo"
        align="left"
        width="180"
      ></el-table-column>
      <el-table-column
        label="油品"
        prop="oilName"
        align="left"
        width="150"
      ></el-table-column>
      <el-table-column label="备注" prop="notes" align="left" width="150">
        <template slot-scope="scope">
          <el-tooltip
            :content="scope.row.notes"
            placement="bottom"
            effect="light"
          >
            <div class="class-notes">{{ scope.row.notes }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { cloneDeep } from "lodash";
import { enterpriseList } from "@/api/station/orderHistory";
import { listEnterpriseStation } from "@/api/dept";
import {
  cdkBalance,
  balanceStatistics,
  rechargeFleetName,
} from "@/api/teamCard/teamCardList";

export default {
  name: "TeamCard_BalanceDetails",
  data() {
    return {
      labelPosition: "right",
      // 车队卡名称列表
      rechargeFleetNameList: [],

      isHide: false,
      // 油企列表
      enterpriseList: [],
      // 油站列表
      stationList: [],
      //时间间隔值
      dateRangeCreatedDate: [],
      // 总条数
      total: 0,
      // 统计数据
      workSum: {
        parentCardNumberBalance: 0,
        sonCardNumberBalance: 0,
        consumptionBalance: 0,
        rechargeBalance: 0,
        giftBalance: 0,
      },
      //表格数据
      orderList: [],
      loading: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 油企id
        enterpriseId: null,
        // 油站id
        stationId: null,
        // 交易类型
        cardTypeList: [],
        // 主卡id
        fleetCardId: null,
        // 主卡号
        parentCardNumber: null,
        // 子卡号
        sonCardNumber: null,
        // 用户姓名
        userName: null,
        // 交易单号
        fleetOrderNo: null,
        // 交易开始时间
        beginTime: null,
        // 交易截止时间
        endTime: null,
        // 手机号
        userPhone: null,
      },
    };
  },
  created() {
    // 油企账号登录
    if (this.presentEnterprise && this.presentEnterprise.enterpriseId) {
      this.queryParams.enterpriseId = this.presentEnterprise.enterpriseId;
      this.getStationList(this.queryParams.enterpriseId);
    }
    // 油站账号登录
    if (this.presentSation) {
      this.queryParams.stationId = this.presentSation;
    }
    this.getList();
    this.getEnterpriseList();
    this.getRechargeFleetName();
  },

  methods: {
    // 选择了时间
    changeDateRangeDate() {
      if (this.dateRangeCreatedDate) {
        // 开始时间
        this.queryParams.beginTime = this.dateRangeCreatedDate[0];
        // 截止时间
        this.queryParams.endTime = this.dateRangeCreatedDate[1];
      } else {
        this.queryParams.beginTime = null;
        // 截止时间
        this.queryParams.endTime = null;
      }
    },
    handleSelectChange(value) {
      this.stationList = [];
      this.queryParams.stationId = null;
      this.getStationList(value);
    },
    // 查询油企
    getEnterpriseList() {
      enterpriseList().then((res) => {
        this.enterpriseList = res.rows;
      });
    },
    // 查询油站
    getStationList(val) {
      listEnterpriseStation({ enterpriseId: val, status: "1" }).then((res) => {
        this.stationList = res;
        // 判断单站，多站
        if (res.length === 1) {
          this.queryParams.stationId = res[0].stationId;
        }
      });
    },

    // 车队卡名称
    getRechargeFleetName() {
      rechargeFleetName(this.queryParams).then((res) => {
        this.rechargeFleetNameList = res.data;
      });
    },

    // 搜索列表
    getList() {
      this.loading = true;
      let queryParams = cloneDeep(this.queryParams);

      let userLevel = this.$store.state.user.userLevel;
      if ([].includes(userLevel)) {
        queryParams.isDeleted = 0;
      }

      if (this.queryParams.cardTypeList.length === 0) {
        queryParams.cardTypeList = null;
      } else {
        queryParams.cardTypeList = this.queryParams.cardTypeList.join(",");
      }

      cdkBalance(queryParams)
        .then((response) => {
          this.loading = false;
          this.orderList = response.rows;
          this.total = response.total;
        })
        .catch(() => {
          this.loading = false;
          this.orderList = [];
          this.total = 0;
        });
      balanceStatistics(queryParams)
        .then((res) => {
          this.loading = false;
          this.workSum.parentCardNumberBalance =
            res.data.parentCardNumberBalance;
          this.workSum.sonCardNumberBalance = res.data.sonCardNumberBalance;
          this.workSum.consumptionBalance = res.data.consumptionBalance;
          this.workSum.rechargeBalance = res.data.rechargeBalance;
          this.workSum.giftBalance = res.data.giftBalance;
        })
        .catch(() => {
          this.loading = false;
          this.workSum.parentCardNumberBalance = 0;
          this.workSum.sonCardNumberBalance = 0;
          this.workSum.consumptionBalance = 0;
          this.workSum.rechargeBalance = 0;
          this.workSum.giftBalance = 0;
        });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreatedDate = [];
      // 开始时间
      this.queryParams.beginTime = null;
      // 截止时间
      this.queryParams.endTime = null;
      this.resetForm("queryForm");
      this.getList();
    },
    //交易金额
    formatRechargeAmount(row) {
      if (row.cardType == "cz" || row.cardType == "imp") {
        return "+" + row.rechargeAmount;
      } else if (row.cardType == "msub" || row.cardType == "cdk") {
        return "-" + row.rechargeAmount;
      }
    },
    // 交易类型
    formatterRefundPay(row) {
      const types = {
        cz: "后台充值",
        imp: "后台增加",
        msub: "后台扣减",
        cdk: "消费核销",
      };
      return types[row.cardType];
    },
  },
};
</script>
<style lang="scss" scoped>
.class-notes {
  text-overflow: ellipsis; /* ellipsis:显示省略符号来代表被修剪的文本  string:使用给定的字符串来代表被修剪的文本*/
  white-space: nowrap; /* nowrap:规定段落中的文本不进行换行   */
  overflow: hidden; /*超出部分隐藏*/
}

.button-from {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .button-style1 {
    display: inline-block;
    width: 80px;

    .el-button {
      margin: 0 0 0 15px;
    }
  }
}

::v-deep .card-table {
  padding: 10px;
  margin-top: 0 !important;
  background-color: #f8f8f8;

  .el-button {
    margin: 0 5px 0 0;
  }
}

// 表格样式
::v-deep .el-form-item {
  margin-bottom: 0;

  .el-form-item__label {
    padding: 0;
    margin: 0;
    height: 29px;
    font-weight: 400;
    font-size: 12px;
  }

  .el-button--cyan {
    margin-center: 20px;
  }
}

::v-deep .el-table {
  border: 1px #f8f8f8 solid;
  border-bottom: 1px #dfe6ec solid;
}

::v-deep .is-center {
  border: 1px transparent solid;
  border-bottom: 1px #f8f8f8 solid;
}

.summary-container {
  width: 100%;
  border: 1px solid #e7e3e3;
  padding: 10px 5px;
  border-radius: 10px;

  margin-bottom: 25px;
  display: flex;
  flex-direction: row;

  .summary-item {
    display: flex;
    flex-direction: column;

    border-right: 1px dashed rgba(0, 0, 0, 0.1);

    .summary-item-box {
      border-bottom: 1px dashed rgba(0, 0, 0, 0.1);
      padding: 5px;
      display: flex;
      justify-items: center;
      align-items: center;
      flex-direction: column;

      .summary-title {
        font-size: 18px;
      }

      .summary-sum {
        font-size: 24px;
        font-weight: 700;
      }
    }
  }

  .summary-item:last-child {
    border-right: none;
  }

  .summary-item-box:nth-last-child(-n + 1) {
    border-bottom: none;
  }
}

.f1 {
  flex: 1;
}
</style>
